<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>

<body>
    <template id="listTemp">
        <div>
            <router-link to='/add'>新增</router-link>
            <button @click='$router.go(1)'>前进</button>
            <ul>
                <router-link tag='li' v-for='item in list' :to="{name:'detail',params:{name:item.name,id:item.id}}">
                    {{item.name}}
                </router-link>
            </ul>
        </div>

    </template>

    <template id="addTemp">
        <div>
            <button @click='back'>返回</button>
            <input type="text">
            <button @click='submit'>提交</button>
        </div>
    </template>

    <div id='app'>
        <router-view></router-view>
    </div>
    <script>
        const list = {
            template: '#listTemp',
            data() {
                return {
                    list: [{ id: 1, name: '奥特曼' }, { id: 2, name: '小怪兽' }, { id: 3, name: '激光波' }]
                }
            }
        }

        const detail = {
            template: `<div>详情组件:{{this.$route.params.name}}{{this.$route.params.id}}</div>`,
            created() {
                console.log(this.$route.params);
            }
        }

        const add = {
            template: '#addTemp',
            methods: {
                add() {
                    console.log('提交成功');
                    // 添加
                    this.$router.push({ path: '/list', query: '{id:123}' })
                    // this.$router.replace({ path: '/list', query: { id: 123 } })
                    // path: 跳转的路径
                    // name： 路由的名字
                    // query: query参数
                    // params： params参数

                    // push
                    // back
                    // go
                    // replace
                },
                back() {
                    // this.$router.back()
                    // 跳转到指定界面
                    this.$router.go(-1)
                }
            }

        }

        const routerIns = new VueRouter({
            routes: [{
                path: '/',
                redirect: '/list'
            },
            {
                path: '/list',
                component: list
            },
            {
                path: '/detail/:name/:id',
                component: detail
            }]
        })

        const vm = new Vue({
            el: '#app',
            data: {
            },
            router: routerIns
        })
    </script>
</body>

</html>